import { notification } from 'antd'

// 错误类型定义
export const ERROR_TYPES = {
    NETWORK_ERROR: 'NETWORK_ERROR',
    VALIDATION_ERROR: 'VALIDATION_ERROR',
    NOT_FOUND: 'NOT_FOUND',
    SERVER_ERROR: 'SERVER_ERROR',
    UPLOAD_ERROR: 'UPLOAD_ERROR'
}

// 网络错误处理
export const handleNetworkError = (error) => {
    console.error('Network error:', error)
    
    let message = '网络连接出现问题'
    let description = '请检查您的网络连接并重试'
    
    if (error.name === 'TypeError' && error.message.includes('fetch')) {
        message = '无法连接到服务器'
        description = '请检查网络连接或稍后重试'
    } else if (error.code === 'ECONNABORTED') {
        message = '请求超时'
        description = '服务器响应时间过长，请稍后重试'
    } else if (error.response) {
        // 服务器返回了错误状态码
        const status = error.response.status
        switch (status) {
            case 400:
                message = '请求参数错误'
                description = '请检查输入的信息是否正确'
                break
            case 401:
                message = '未授权访问'
                description = '请登录后重试'
                break
            case 403:
                message = '访问被拒绝'
                description = '您没有权限执行此操作'
                break
            case 404:
                message = '资源不存在'
                description = '请求的资源未找到'
                break
            case 500:
                message = '服务器内部错误'
                description = '服务器遇到问题，请稍后重试'
                break
            default:
                message = `服务器错误 (${status})`
                description = '请稍后重试或联系技术支持'
        }
    }
    
    notification.error({
        message,
        description,
        duration: 5,
        placement: 'topRight'
    })
}

// API 错误处理
export const handleApiError = (error, customMessage = null) => {
    console.error('API error:', error)
    
    if (customMessage) {
        notification.error({
            message: '操作失败',
            description: customMessage,
            duration: 4,
            placement: 'topRight'
        })
        return
    }
    
    // 如果是网络错误，使用网络错误处理
    if (!error.response) {
        handleNetworkError(error)
        return
    }
    
    // 处理 API 返回的错误
    const { status, data } = error.response
    let message = '操作失败'
    let description = '请稍后重试'
    
    if (data && data.message) {
        description = data.message
    } else if (data && data.error) {
        description = data.error
    }
    
    switch (status) {
        case 400:
            message = '请求无效'
            break
        case 401:
            message = '未授权'
            break
        case 403:
            message = '权限不足'
            break
        case 404:
            message = '资源不存在'
            break
        case 422:
            message = '数据验证失败'
            break
        case 500:
            message = '服务器错误'
            break
    }
    
    notification.error({
        message,
        description,
        duration: 4,
        placement: 'topRight'
    })
}

// 文件上传错误处理
export const handleUploadError = (error, fileName = '') => {
    console.error('Upload error:', error)
    
    let message = '文件上传失败'
    let description = fileName ? `文件 "${fileName}" 上传失败` : '文件上传失败'
    
    if (error.message) {
        if (error.message.includes('size')) {
            description = '文件大小超出限制，请选择较小的文件'
        } else if (error.message.includes('type')) {
            description = '文件类型不支持，请选择正确的文件格式'
        } else {
            description = error.message
        }
    }
    
    notification.error({
        message,
        description,
        duration: 5,
        placement: 'topRight'
    })
}

// 成功提示
export const showSuccess = (message, description = null) => {
    notification.success({
        message,
        description,
        duration: 3,
        placement: 'topRight'
    })
}

// 警告提示
export const showWarning = (message, description = null) => {
    notification.warning({
        message,
        description,
        duration: 4,
        placement: 'topRight'
    })
}

// 信息提示
export const showInfo = (message, description = null) => {
    notification.info({
        message,
        description,
        duration: 3,
        placement: 'topRight'
    })
}